const create_standard_menu = async () => {
  const { getState } = require("./db/state");

  const state = getState();
  await state?.refresh_config(false);
  const allow_signup = state.getConfig("allow_signup");
  const notification_in_menu = state.getConfig("notification_in_menu");
  const login_menu = state.getConfig("login_menu");
  const canEditTables = state.getConfig("min_role_edit_tables", 1);
  const canInspectTables = state.getConfig("min_role_inspect_tables", 1);
  const canEditViews = state.getConfig("min_role_edit_views", 1);
  const canEditPages = state.getConfig("min_role_edit_pages", 1);
  const canEditTriggers = state.getConfig("min_role_edit_triggers", 1);
  const canEditMenu = state.getConfig("min_role_edit_menu", 1);
  const canEditFiles = state.getConfig("min_role_edit_files", 1);
  const canEditSearch = state.getConfig("min_role_edit_search", 1);

  const hasAdmin = Math.max(
    canEditMenu,
    canEditTriggers,
    canEditFiles,
    canEditSearch
  );
  const new_items = [
    {
      href: "",
      icon: "fas fa-table",
      text: "Tables",
      type: "Admin Page",
      label: "Tables",
      style: "",
      title: "",
      target: "_self",
      tooltip: "",
      in_modal: false,
      location: "Standard",
      max_role: "1",
      min_role: Math.max(canEditTables, canInspectTables),
      admin_page: "Tables",
      target_blank: false,
      disable_on_mobile: true,
    },
    {
      href: "",
      icon: "fas fa-eye",
      text: "Views",
      type: "Admin Page",
      label: "Views",
      style: "",
      title: "",
      target: "_self",
      tooltip: "",
      in_modal: false,
      location: "Standard",
      max_role: "1",
      min_role: canEditViews,
      admin_page: "Views",
      target_blank: false,
      disable_on_mobile: true,
    },
    {
      href: "",
      icon: "far fa-file",
      text: "Pages",
      type: "Admin Page",
      label: "Pages",
      style: "",
      title: "",
      target: "_self",
      tooltip: "",
      in_modal: false,
      location: "Standard",
      max_role: "1",
      min_role: canEditPages,
      admin_page: "Pages",
      target_blank: false,
      disable_on_mobile: true,
    },
    {
      href: "",
      icon: "fas fa-wrench",
      text: "Settings",
      type: "Header",
      label: "Settings",
      style: "",
      title: "",
      target: "_self",
      tooltip: "",
      location: "Standard",
      max_role: "1",
      min_role: hasAdmin,
      subitems: [
        {
          href: "",
          icon: "fas fa-tools",
          text: "About application",
          type: "Admin Page",
          label: "About application",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "1",
          admin_page: "About application",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-cubes",
          text: "Modules",
          type: "Admin Page",
          label: "Modules",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "1",
          admin_page: "Modules",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-users-cog",
          text: "Users and security",
          type: "Admin Page",
          label: "Users and security",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "1",
          admin_page: "Users and security",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-compass",
          text: "Site structure",
          type: "Admin Page",
          label: "Site structure",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: Math.max(canEditSearch, canEditMenu),
          admin_page: "Site structure",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-images",
          text: "Files",
          type: "Admin Page",
          label: "Files",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: canEditFiles,
          admin_page: "Files",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-calendar-check",
          text: "Events",
          type: "Admin Page",
          label: "Events",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: canEditTriggers,
          admin_page: "Events",
          target_blank: false,
          disable_on_mobile: false,
        },
        {
          href: "",
          icon: "fas fa-list",
          text: "All entities",
          type: "Admin Page",
          label: "All entities",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "1",
          admin_page: "Entities",
          target_blank: false,
          disable_on_mobile: false,
        },
      ],
      user_menu_header: false,
      disable_on_mobile: true,
    },
    ...(allow_signup
      ? [
          {
            href: "",
            icon: "fas fa-user-plus",
            text: "Sign up",
            type: "User Page",
            label: "Sign up",
            style: "",
            title: "",
            target: "_self",
            tooltip: "",
            in_modal: false,
            location: "Standard",
            max_role: "100",
            min_role: "100",
            user_page: "Signup",
            target_blank: false,
            disable_on_mobile: true,
          },
        ]
      : []),
    ...(login_menu
      ? [
          {
            href: "",
            icon: "fas fa-sign-in-alt",
            text: "Login",
            type: "User Page",
            label: "Login",
            style: "",
            title: "",
            target: "_self",
            tooltip: "",
            in_modal: false,
            location: "Standard",
            max_role: "100",
            min_role: "100",
            user_page: "Login",
            target_blank: false,
            disable_on_mobile: true,
          },
        ]
      : []),
    {
      href: "",
      icon: "far fa-user",
      text: "User",
      type: "Header",
      label: "User",
      style: "",
      title: "",
      target: "_self",
      tooltip: "",
      in_modal: false,
      location: "Standard",
      max_role: "1",
      min_role: "80",
      subitems: [
        ...(notification_in_menu
          ? [
              {
                href: "",
                icon: "far fa-bell",
                text: "Notifications",
                type: "User Page",
                label: "Notifications",
                style: "",
                title: "",
                target: "_self",
                tooltip: "",
                in_modal: false,
                location: "Standard",
                max_role: "1",
                min_role: "80",
                user_page: "Notifications",
                target_blank: false,
                disable_on_mobile: true,
              },
            ]
          : []),
        {
          href: "",
          icon: "fas fa-user-cog",
          text: "User settings",
          type: "User Page",
          label: "User settings",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "80",
          user_page: "User settings",
          target_blank: false,
          disable_on_mobile: true,
        },
        {
          href: "",
          icon: "fas fa-sign-out-alt",
          text: "Logout",
          type: "User Page",
          label: "Logout",
          style: "",
          title: "",
          target: "_self",
          tooltip: "",
          in_modal: false,
          location: "Standard",
          max_role: "1",
          min_role: "80",
          user_page: "Logout",
          target_blank: false,
          disable_on_mobile: true,
        },
      ],
      user_menu_header: true,
      disable_on_mobile: true,
    },
  ];

  const menu = state.getConfigCopy("menu_items") || [];
  await state.setConfig("menu_items", [...menu, ...new_items]);
  const unrolled_menu = state.getConfigCopy("unrolled_menu_items") || [];
  await state.setConfig("unrolled_menu_items", [
    ...unrolled_menu,
    ...new_items,
  ]);
};

module.exports = create_standard_menu;
